﻿using DFGK.IService.LY;
using DFGK.Service.LY;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models.LyModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.PC.装备大数据统计
{
    /// <summary>
    /// 人装匹配器材
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class MatchingEquipmentController : BaseController
    {
        private readonly IMatchingEquipmentService matchingEquipmentService;
        public MatchingEquipmentController(IWebHostEnvironment environment, IMatchingEquipmentService matchingEquipmentService) : base(environment)
        {
            this.matchingEquipmentService = matchingEquipmentService;
        }
        /// <summary>
        /// 人装匹配器材添加或修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddOrModefiyMatchingEquipment(MatchingEquipment model)
        {
            if (model.Id > 0)
            {
                model.ModifyDate = DateTime.Now;
                model.ModifyUserId = base.BaseGetNowUserId();
                model.ModifyUserName = base.BaseGetNowUserName();
                bool result = matchingEquipmentService.AddOrModefiyMatchingEquipment(model, out string message);
                if (result)
                {
                    return Ok(BaseResult.Result(true, "修改成功！", "200"));
                }
                return Ok(BaseResult.Result(false, "修改失败！" + message, "500"));
            }
            else
            {
                model.CreateUserName = base.BaseGetNowUserName();
                model.CreateUserId = base.BaseGetNowUserId();
                model.CreateDate = DateTime.Now;
                bool result = matchingEquipmentService.AddOrModefiyMatchingEquipment(model, out string message);
                if (result)
                {
                    return Ok(BaseResult.Result(true, "添加成功！", "200"));
                }
                return Ok(BaseResult.Result(false, "添加失败！" + message, "500"));


            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="matchingEquipmentId"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelMatchingEquipment(long matchingEquipmentId)
        {
            if (matchingEquipmentId <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var result = matchingEquipmentService.DelMatchingEquipment(matchingEquipmentId);
            if (result)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMatchingEquipmentData(string? name,  int pageIndex = 1, int pageSize = 15)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = matchingEquipmentService.GetMatchingEquipmentData(name, out var totalCount, pageIndex, pageSize);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 根据id查询
        /// </summary>
        /// <param name="matchingEquipmentId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMatchingEquipment(long matchingEquipmentId)
        {
            if (matchingEquipmentId <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var shareInfo = matchingEquipmentService.GetMatchingEquipment(matchingEquipmentId);
            if (shareInfo != null)
            {
                return Ok(BaseResult.Result(shareInfo, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
    }
}
